<?php

namespace app\admin\model;

use app\common\enums\StatusEnum;
use think\Db;

class LangClassReciteType extends \app\common\model\LangClassReciteType
{
    /**
     * 列表
     * @param $classId
     * @param $courseType
     * @param $type
     * @param $where
     * @return array
     * @throws \think\exception\DbException
     */
    public static function getList($classId, $courseType, $type, $where)
    {
        $sql = static::field('id, type, name, status, sort_num, create_time, update_time')
            ->where(['class_id' => $classId, 'course_type' => $courseType, 'type' => $type]);

        if ($where) {
            $sql->where($where);
        }

        $data = $sql->paginate(null, false, ['query' => request()->param()]);

        $model = $data->each(function ($item, $key) {
            $item['create_time'] = get_datetime($item['create_time']);
            $item['update_time'] = get_datetime($item['update_time']);
            $item['statusText'] = StatusEnum::getStatus($item['status']);
            return $item;
        });

        // 获取总记录数
        $count = $model->total();
        return [
            'model' => $model,
            'count' => $count
        ];
    }

    /**
     * id => name
     * @param array $typeIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getColumn($typeIds = [])
    {
        $sql = static::field('id, name')->where(['status' => StatusEnum::ENABLED]);
        if ($typeIds) {
            $sql->where('id', 'in', $typeIds);
        }
        $model = $sql->order(['sort_num' => 'desc', 'create_time' => 'desc'])->select()->toArray();
        $result = [];
        foreach ($model as $key => $value) {
            $result[$value['id']] = $value['name'];
        }
        return $result;
    }


    /**
     * 删除
     * @param $id
     */
    public static function deleteOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->delete();
        }
    }

    public static function deleteAll($ids)
    {
        static::where('id', 'in', $ids)->delete();
    }

    /**
     * 修改状态
     * @param $id
     */
    public static function disabledOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->status = ($model->status == StatusEnum::ENABLED) ? StatusEnum::DISABLED : StatusEnum::ENABLED;
            $model->update_time = time();
            $model->save();
        }
    }

    /**
     * 批量禁用启用
     * @param $ids
     * @param $status
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function disables($ids, $status)
    {
        static::where('id', 'in', $ids)->update(['status' => $status]);
    }

    /**
     * 添加修改贴吧分类
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function saveOne($data)
    {
        $saveData = [
            'class_id' => $data['class_id'],
            'course_type' => $data['course_type'],
            'type' => $data['type'],
            'name' => $data['name'],
            'status' => $data['status'],
            'sort_num' => $data['sort_num'],
            'update_time' => time()
        ];

        if (isset($data['id']) && $data['id'] > 0) {
            Db::name('lang_class_recite_type')
                ->where('id', $data['id'])
                ->data($saveData)
                ->update();
            $id = $data['id'];
        } else {
            $saveData['create_time'] = time();
            $id = Db::name('lang_class_recite_type')->insertGetId($saveData);
        }
        return $id;
    }
}